angular.module('seller-address', [])

.controller 'SellerAddress', ($scope,userData, $state,areaData,mallInfo, $http,$stateParams, $ionicHistory,showToast) ->
    _.extend $scope,
        info:
            address: undefined
            city:undefined
            province:undefined
            cityName:undefined
            provinceName:undefined
        goBack: ->
            $ionicHistory.goBack()
        doSave: ->
            if $scope.oriAddr==$scope.info.address and  $scope.info.city==mallInfo.mall.city#未修改地址,直接返回
                $ionicHistory.goBack()
                return
            postData={
                action:'editshopinfo'
                userid:userData.getToken()
                phone:''
                province:$scope.info.province
                city:$scope.info.city
                address:$scope.info.address
            }
            $http.post('/utf-8data/',postData)
            .success (resp) ->
                console.log resp
                if resp.status=='0'
                    showToast '修改商家地址成功'
                    mallInfo.mall.province=$scope.info.province
                    mallInfo.mall.city=$scope.info.city
                    mallInfo.mall.address=$scope.info.address
                    $ionicHistory.goBack()
                else
                    showToast resp.msg
            .error (err) ->
                showToast '修改商家地址失败'
        clear: () ->
            $scope.info.address = ''
        goProvince:()->
            return if !areaData.provinceList #未获取到areadata
            $state.go 'tab.province-data-myself'
        goCity:()->
            return if !areaData.provinceList #未获取到areadata
            $state.go 'tab.city-data-myself',{provinceData:$scope.theProvince}
    loadProvinceData=()->
        $scope.info.province=mallInfo.mall.province
        $scope.info.city=mallInfo.mall.city
        if areaData.provinceList
            $scope.theCity=_.find areaData.cityList,(city)-> city.cityid==$scope.info.city
            $scope.theProvince=_.find areaData.provinceList,(province)-> province.provinceid==$scope.info.province
            $scope.info.provinceName=$scope.theProvince.provincename
            $scope.info.cityName=$scope.theCity.cityname
        else
            $http.get('/seller/',params:{action:'areadata'})
            .success (resp) ->
                if resp.status == '0' and resp.body?.length>=2
                    areaData.provinceList = resp.body[0].province
                    areaData.cityList=resp.body[1].city
                    $scope.theCity=_.find areaData.cityList,(city)-> city.cityid==$scope.info.city
                    $scope.theProvince=_.find areaData.provinceList,(province)-> province.provinceid==$scope.info.province
                    $scope.info.provinceName=$scope.theProvince.provincename
                    $scope.info.cityName=$scope.theCity.cityname
                else
                    showToast '获取地区信息失败'
            .error (err) ->
                showToast '获取地区信息失败'
    $scope.$on '$ionicView.enter', ->
        $scope.oriAddr=mallInfo.mall.address
        $scope.info.address=mallInfo.mall.address
        if areaData.city
            $scope.info.province=areaData.province.provinceid
            $scope.info.city=areaData.city.cityid
            $scope.theProvince=areaData.province
            $scope.theCity=areaData.city
            $scope.info.provinceName=$scope.theProvince.provincename
            $scope.info.cityName=$scope.theCity.cityname
            areaData.province=undefined
            areaData.city=undefined
        else if !$scope.info.province or !$scope.info.city
            loadProvinceData()
    $scope.$on '$stateChangeSuccess', (evnet,toState)->
        if toState?.name=='tab.myself'
            $scope.info.address=''
            $scope.info.city=''
            $scope.info.provinceName=''
            $scope.info.cityName=''
            $scope.info.province=''
            $scope.oriAddr=''



